/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ia_psr;

import ia_psr.algoritmos.IAlgoritmo;
import ia_psr.problemas.IConjunto;
import ia_psr.problemas.IProblema;
import ia_psr.graph.GrafoRestricciones;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
public class PSRSolver {
    private IAlgoritmo         a;
    private IConjunto          c;
    private GrafoRestricciones g;
    private IProblema          p;
    private boolean            solucion;
    private static PSRSolver   instance;

    public static PSRSolver getInstance() {
        if (instance == null)
            instance = new PSRSolver();
        return instance;
    }

    private PSRSolver() {
        solucion = false;
    }

    public boolean hasSolution() {
        return a.hasSolution();
    }

    public void initSolver(GrafoRestricciones g, IProblema p, IConjunto c, IAlgoritmo a) {
        this.g = g;
        this.p = p;
        this.c = c;
        this.a = a;
        solucion = false;
    }


    public GrafoRestricciones solve() {
        c.resetVariables();
        g.setProblema(p);
        a.resolver(g, c);
        
        solucion = a.hasSolution();

        return g;
    }

    public void printSolucion() {
        System.out.println("Solucion: ");
        g.print();
        System.out.println("Nodos expandidos: " + a.getNodosExpandidos());
        System.out.println("Variables expandidas: " + a.getVariablesExpandidas());
        System.out.println("Tiempo de ejecucion: " + a.getExecutionTime());
    }

    public void setGrafo(GrafoRestricciones g) {
        this.g = g;
    }

    public void setConjunto(IConjunto c) {
        this.c = c;
    }

    public void setProblema(IProblema p) {
        this.p = p;
    }

    public void setAlgoritmo(IAlgoritmo a) {
        this.a = a;
    }

    public GrafoRestricciones getGrafo() {
        return g;
    }

}
